#include "xdata/xdataform.h"

namespace yoohoo_xdata {

XDataForm::XDataForm(XDataForm::Type type) : _type(type) {}

void XDataForm::setTitle(QString title) {
	_title = title;
}

QString XDataForm::title() {
	return _title;
}

void XDataForm::setInstructions(QString instructions) {
	_instructions = instructions;
}

QString XDataForm::instructions() {
	return _instructions;
}

void XDataForm::setReported(XDataReported reported) {
	_reported = reported;
}

XDataReported& XDataForm::reported() {
	return _reported;
}
void XDataForm::setItems(QList<XDataItem> items) {
	_items = items;
}

QList<XDataItem>& XDataForm::items() {
	return _items;
}

void XDataForm::setType(XDataForm::Type type) {
	_type = type;
}

XDataForm::Type XDataForm::type() {
	return _type;
}

QString XDataForm::typeString() {
	if (_type == XDataForm::FORM) {
		return "form";
	} else if (_type == XDataForm::RESULT) {
		return "result";
	} else if (_type == XDataForm::SUBMIT) {
		return "submit";
	} else {
		return "cancel";
	}
}

void XDataForm::clear() {
	_type = FORM;
	_title.clear();
	_instructions.clear();
	_reported.fields().clear();
	_items.clear();
}

QString XDataForm::toXml() {
	QString xml;

	xml.append("<x xmlns='jabber:x:data'");
	if (_type == XDataForm::FORM) {
		xml.append(QString(" type='%1'>").arg("form"));
	} else if (_type == XDataForm::RESULT) {
		xml.append(QString(" type='%1'>").arg("result"));
	} else if (_type == XDataForm::CANCEL) {
		xml.append(QString(" type='%1'>").arg("cancel"));
	} else {
		xml.append(QString(" type='%1'>").arg("submit"));
	}

	if (!_title.isEmpty()) {
		xml.append(QString("<title>%1</title>").arg(_title));
	}

	if (!_instructions.isEmpty()) {
		xml.append(QString("<instructions>%1</instructions>").arg(_instructions));
	}

	xml.append(XDataFieldContainer::toXml());

	foreach (XDataItem item, _items) {
		xml.append(item.toXml());
	}

	xml.append(_reported.toXml());

	xml.append("</x>");

	return xml;
}

}
